/*
 * CategoryFragment.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.category;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseEventFragment;
import com.yidiandao.business.ui.adapter.CategoryListAdapter;
import com.yidiandao.business.ui.adapter.CategoryTreeInfoAdapter;
import com.yidiandao.business.ui.searchabout.searchbrandandaddress.SearchBrandAndAddressActivity;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/**
 * 首页 分类
 */
public class CategoryFragment extends SuperBaseEventFragment
        implements CategoryContract.View {

    public static final String TAG = CategoryFragment.class.getSimpleName();

    private CategoryContract.Presenter mPresenter;

    private ListView mListView;

    private ListView mListViewInfo;

    private CategoryListAdapter mCategoryListAdapter;

    private CategoryTreeInfoAdapter mCategoryTreeInfoAdapter;

    private TextView mIbtnSearch;

    public static CategoryFragment getInstance() {
        return new CategoryFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (null != savedInstanceState) {
            mCategoryListAdapter = new CategoryListAdapter(mActivity);
            mCategoryTreeInfoAdapter = new CategoryTreeInfoAdapter(mActivity);
        }
    }

    @Override
    protected void initViews() {
        mListView = (ListView) mRootView.findViewById(R.id.list_view);
        mListViewInfo = (ListView) mRootView.findViewById(R.id.list_view_right);

        mIbtnSearch = (TextView) mRootView.findViewById(R.id.ibtn_search);

        initAdapter();
        initEvent();
    }

    private void initAdapter() {
        if (mCategoryListAdapter == null)
            mCategoryListAdapter = new CategoryListAdapter(mActivity);
        mListView.setAdapter(mCategoryListAdapter);

        if (mCategoryTreeInfoAdapter == null) {
            mCategoryTreeInfoAdapter = new CategoryTreeInfoAdapter(mActivity);
        }
        mListViewInfo.setAdapter(mCategoryTreeInfoAdapter);
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }

    private void initEvent() {
        mIbtnSearch.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SearchBrandAndAddressActivity.actionStart(mActivity);
                    }
                });

        mListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(
                            AdapterView<?> parent, View view, int position, long id) {
                        mPresenter.setData(position);
                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_category;
    }

    @Override
    public void setPresenter(CategoryContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return TAG;
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    public void setCategoryBrand(List<DisplayListItem> disPlayListItems) {
        mCategoryListAdapter.setData(disPlayListItems);
    }

    @Override
    public void setCategoryInfo(List<DisplayListItem> disPlayListItems) {
        mCategoryTreeInfoAdapter.setData(disPlayListItems);
    }

    @Override
    public void refreshUI() {
        mCategoryListAdapter.notifyDataSetChanged();
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    @Override
    public void notifyDataChanged() {
        mPresenter.start();
    }
}
